package com.aofeng.hybridwork.android.task;

import android.app.ProgressDialog;
import android.os.AsyncTask;

import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.service.SqlService;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 保存已超标未缴费数据
 * Created by LGY on 2016-08-25.
 */
public class SavePaydata extends AsyncTask<String, String, Boolean> {
    HybridActivity hybridActivity;
    Boolean param;
    JSONArray js;



    public SavePaydata(HybridActivity hybridActivity) {
        this.hybridActivity = hybridActivity;
    }

    @Override
    /**
     * params:
     * 保存抄表数据
     */
    protected Boolean doInBackground(String... params) {
        try {
            param = new Boolean(params[1]);
            js = new JSONArray(params[0]);
            SqlService ss = new SqlService(hybridActivity);
            JSONObject jo;
            for (int i = 1; i <= js.length(); i++) {
                jo =js.getJSONObject(i-1);
                //true 单个保存已经收费记录 false 批量保存已抄表未收费数据
                if (param)
                   ss.run("update t_handplan set f_oughtfee = '" + jo.getInt("f_oughtfee")   +  "',f_input_date = '" + jo.getString("f_input_date") +  "',f_upload = '已上传 ',f_whether_pay = '是' where f_userinfo_id =  '" + jo.getInt("f_userinfo_id") +  "' and f_savehangid= '" + jo.getInt("f_user_id") + "'");
                else
                    ss.run("update t_handplan set f_input_date = '" + jo.getString("f_input_date") +  "',f_upload = '已上传',f_whether_pay = '否' where f_userinfo_id =  '" + jo.getInt("f_userinfo_id") +  "' and f_savehangid= '" + jo.getInt("f_user_id") + "'");
            }
            return true;
        } catch(Exception e) {
            e.printStackTrace();
            return false;
        }
    }






    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        hybridActivity.createProgressDialog(ProgressDialog.STYLE_SPINNER);
    }

    @Override
    protected void onPostExecute(Boolean flag) {
        super.onPostExecute(flag);
        hybridActivity.closeProgress();
        hybridActivity.mWebView.loadUrl("javascript:HostApp.__callback__(" +  flag + ","+ param +","+js +");");
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        int progress = Integer.parseInt(values[0]);
        hybridActivity.setProgress(progress, values[1]);
    }
}
